package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bo.CommentListBO;
import com.cskaoyan.module.pojo.MarketComment;
import com.cskaoyan.module.pojo.MarketCommentExample;
import com.cskaoyan.module.vo.CommentData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 创建日期: 2022/04/10 13:10
 *
 * @author Administrator
 */
@Service
public class CommentServiceImpl implements CommentService{
   @Autowired
    MarketCommentMapper marketCommentMapper;


    @Override
    public BaseRespVo commentList(CommentListBO commentListBO) {
        Integer page = commentListBO.getPage(); // 页码
        Integer limit = commentListBO.getLimit(); // 数据量
        String sort = commentListBO.getSort(); // 排序列
        String order = commentListBO.getOrder(); // dsec\asc

        // 分页插件 PageHelper,辅助我们作分页和分页信息的获得
        //执行查询过程中拼接分页信息
        PageHelper.startPage(page, limit);
        Integer userId = commentListBO.getUserId();
        Integer valueId = commentListBO.getValueId();
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        example.setOrderByClause(sort + " " + order);
        List<MarketComment> list = marketCommentMapper.selectByExample(example);

        PageInfo<MarketComment> commentPageInfo = new PageInfo<>(list);
        long total = commentPageInfo.getTotal();
        int pages = commentPageInfo.getPages();
        CommentData commentData = new CommentData();
        commentData.setLimit(limit);
        commentData.setPage(page);
        commentData.setList(list);
        commentData.setPages(pages);
        commentData.setTotal(total);

        return BaseRespVo.ok(commentData);
    }

    @Override
    public BaseRespVo deleteComment(MarketComment marketComment) {
        Integer id = marketComment.getId();
        int i = marketCommentMapper.deleteByPrimaryKey(id);
        return BaseRespVo.ok("成功");
    }
}
